<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" import="java.util.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript" type="text/javascript">
// <!CDATA[


function placebid() {
	
	document.myForm.target.value = "bid";
	document.myForm.submit();
}

//]]>
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <meta http-equiv="Pragma" content="no-cache">
 <meta http-equiv="Cache-Control" content="no-cache">
 <meta http-equiv="Expires" content="Sat, 01 Dec 2001 00:00:00 GMT">
<title>Auction Page</title>
</head>
<body>
<table width="100%" height="100%" border="2">
  <tr>
    <td width="22%" height="95"><img src="logo.png" width="163" height="154" alt="Logo" /></td>
    <td width="78%"><h1>Island Auctions</h1>
    <p> Turning Junk Into Money</p></td>
  </tr>
  <tr>
    <td valign="top"><p><a href="index.jsp">Home</a></p>
    <p><a href="Browse.jsp">Browse</a></p>
    <p><a href="Search.jsp">Search</a></p>
    <p><a href="profile.jsp">Profile</a></p>
    <p><a href="Users.jsp">Users</a></p>
    <%
    try{
	    if(session.getValue("login") != null)
	    {%>
	    	<p><a href="Logout.jsp">Logout</a></p>
	    <%}
	    else
	    {%>
	    	<p><a href="login.jsp">Login</a></p>
	    <%}
    }catch(java.lang.IllegalStateException e)
    {%>
		<p><a href="login.jsp">Login</a></p><%
}%>

<%
try{
    if(session.getValue("empLevel") == "1")
    {%>
    	<p><a href="ManageEmployees.jsp">(Manage Employees)</a></p></td>
    <%}
    else
    {%>
    <br>
    <%}
}catch(java.lang.IllegalStateException e)
{%>
<br>
<%}
    %>
    <td valign="top">
       	<%
String mysJDBCDriver = "com.mysql.jdbc.Driver"; 
String mysURL = "jdbc:mysql://mysql2.cs.stonybrook.edu:3306/jedale"; 
String mysUserID = "jedale"; 
String mysPassword = "107200855";
float currentBid;
String AuctionID =request.getParameter("AuctionID");
if(AuctionID == null)
	response.sendRedirect("index.jsp");
String processBid = request.getParameter("target");
String BidPrice = request.getParameter("BidPrice");
String MinBidPrice = request.getParameter("mustBe");
  			  java.sql.Connection conn=null;
  			
			try 
			{
            	Class.forName(mysJDBCDriver).newInstance();
    			java.util.Properties sysprops=System.getProperties();
    			sysprops.put("user",mysUserID);
    			sysprops.put("password",mysPassword);
        
				//connect to the database
            			conn=java.sql.DriverManager.getConnection(mysURL,sysprops);
            			System.out.println("Connected successfully to database using JConnect");
            
            			java.sql.Statement stmt1=conn.createStatement();
            			java.sql.Statement stmt2=conn.createStatement();
            			java.sql.Statement stmt3=conn.createStatement();
            			if(processBid!=null)
            			{
            			if(processBid.equals("bid"))
            			{
            				java.sql.ResultSet rs2 = stmt2.executeQuery("select HighBid from HighBid Where AuctionID="+AuctionID);
            				if(session.getValue("customerID") == null)
            					response.sendRedirect("login.jsp");
            				
            				
            				int cId = (Integer)session.getValue("customerID");
            				float bid = Float.parseFloat(BidPrice);
            				int aId = Integer.parseInt(AuctionID);
            				float mBid = Float.parseFloat(MinBidPrice);
            				if(mBid <= bid)
            				{
	            				stmt1.executeUpdate("INSERT INTO Bid (CustomerID,AuctionID,BidTime,BidPrice) "+
	            									"VALUES("+cId+","+aId+",CURRENT_TIMESTAMP,"+bid+")");
            				}
            				else
            				{
            					%>
            					<p>Your bid was too low. Please don't do that again.</p>
            					<%
            					
            				}
            				
            			}
            			}
            			
            	
        
					java.sql.ResultSet rs = stmt1.executeQuery
							("SELECT Item.Name, Item.ItemType , Item.ItemDescription, Item.NumCopies, " +
									"Post.PostDate, Post.ExpireDate,Person.Username , Auction.minimumbid, Auction.BidIncrement " +
							  "FROM Auction, Post, Item , Customer, Person " + 
							  "WHERE Item.ItemID =Auction.ItemID and Post.AuctionID=Auction.AuctionID AND " +
							  		"Customer.CustomerID=Post.CustomerID and Person.SSN=Post.CustomerID and " + 
							  		"Auction.AuctionID='" +AuctionID+"'");
					 %>
				<h1>Auction</h1>
	
			
									
		<table name="itemTable">					<%     	
		
		if(rs.next())
		{
				float f1 = rs.getFloat(8);
				String nextBid= String.valueOf(f1);

				String hBid = String.valueOf(rs.getFloat(8));
				java.sql.ResultSet rs2 = stmt2.executeQuery("select HighBid from HighBid Where AuctionID="+AuctionID);
				if(rs2.next())
				{
					nextBid= String.valueOf(rs2.getFloat(1) + rs.getFloat(9));
					hBid = String.valueOf(rs2.getFloat(1));
				}
		
		
      	 // while(rs.next())
        //	{
%>
					<tr>
					<td><span style="font-size: 12pt">Item Name: 	</span></td>
					<td><span ><%=rs.getString(1)%></span></td>
					</tr>
					<tr>
					<td><span style="font-size: 12pt">Item Type: 	</span></td>
					<td><span ><%=rs.getString(2)%></span></td>
					</tr>	
					<tr>
					<td><span style="font-size: 12pt">Description: 	</span></td>
					<td><span ><%=rs.getString(3)%></span></td>
					</tr>
					<tr>
					<td><span style="font-size: 12pt">Copies: 	</span></td>
					<td><span ><%=rs.getString(4)%></span></td>
					</tr>
					<tr>
					<td><span style="font-size: 12pt">Post Date	: 	</span></td>
					<td><span ><%=rs.getString(5)%></span></td>
					</tr>
					<tr>
					<td><span style="font-size: 12pt">Expiration Date	: 	</span></td>
					<td><span ><%=rs.getString(6)%></span></td>
					</tr>
					<tr>
					<td><span style="font-size: 12pt">Seller		: 	</span></td>
					<td><span ><%=rs.getString(7)%></span></td>
					</tr>
					<tr>
					<td><span style="font-size: 12pt">Current Bid		: 	</span></td>
					<td><span >$<%=hBid%></span></td>
					</tr>
					<%
					
					java.sql.ResultSet rs3 = stmt3.executeQuery("select * from Sold Where AuctionID="+AuctionID);
					
					if(rs3.next())
					{
						%>
						<td><span style="font-size: 12pt">Auction Closed	</span></td>
						
						<%
						
					}
					else	
					{
	%>		
	<tr>
	<td><span style="font-size: 12pt">Place Bid		: 	</span></td>
	<form name="myForm" action="AuctionPage.jsp?AuctionID=<%=AuctionID%>" method="post">
	<input type="hidden" value="nobid" name="target"></input>
						<input type="hidden" value="<%=nextBid%>" name="mustBe"></input>
	<td><input name="BidPrice" value="<%=nextBid%>" id="PlacedBid" style="font-size: 10pt; width: 50px; left: 1px; position: relative; top: 0px;" type="text" /></td>
	<td><input name="BidButton" id="buttonBid" type="button" value="PlaceBid" onclick="placebid()"/></td>
	
		<%			
					}
					
					
					
					
					%>
					
					

					</form>
					</tr>
				
					
                  
			</table>					
			<%      		
        	}
  			} catch(Exception e)
  			//<%currentBid=Integer.parseInt(rs.getString(8));
			{
				e.printStackTrace();
				out.print(e.toString());
			}
			finally{
			
				try{conn.close();}catch(Exception ee){};
			}
			
			

  %>
    </td>
  </tr>
</table>
</body>
</html>
